<?php
include_once "dbconnect.php";

function saveResume($username,$name,$htmlstruct){
    $link = initDBconnect();

    $sql = "INSERT INTO resume (name ,htmlstruct,user_name ) values('$name','$htmlstruct','$username')";
    $result = mysqli_query($link,$sql);     //插入操作返回的是布尔值
    if($result){
        return 1;
    }else{
        return 0;
    }
}

// 查询Resume记录，生成列表
function queryResumeList(){
    $link = initDBconnect();

    $sql = "SELECT id,user_name,name FROM resume";
    $result = mysqli_query($link,$sql);

    while($row = mysqli_fetch_assoc($result)){
        // $res[] = $row;
        // $res[] = Array(
        //     "user_name" => $row["user_name"],
        //     "name" => $row["name"]
        // );
        // $res["user_name"] = $row["user_name"];
        // $res["name"] = $row["name"];
        // echo json_encode($row,JSON_UNESCAPED_UNICODE );
        // echo json_encode($res,JSON_UNESCAPED_UNICODE );
        // var_dump($row["name"]);
        // echo $row["name"];

        $str=<<<EOH
        <li class="list-group-item">
        <span>{$row["user_name"]}</span>
        <a href="updateresume.php?id={$row["id"]}" style="margin-left: 150px;">{$row["name"]}</a>
        <button class="badge btn btn-danger" onclick="deleteResume({$row["id"]})" style="float: right">删除</button>
    </li>
EOH;
        echo $str;
    }
    
    // echo $res;
    // var_dump($res);
    // echo json_encode($res,JSON_UNESCAPED_UNICODE );
    // return json_encode($res,JSON_UNESCAPED_UNICODE );
}
// queryResumeList();

// 删除指定resume记录
function deleteResume($id){
    $link = initDBconnect();

    $sql = "DELETE  FROM resume where id=$id";
    mysqli_query($link,$sql);
    $result = mysqli_affected_rows($link);

    // var_dump($result);
    if($result){
        return 1;
    }else{
        return 0;
    }
}

// 更新指定resume记录
function updateResume($id,$name, $htmlstruct){
    $link = initDBconnect();

    $sql = "UPDATE resume  set name='$name',htmlstruct='$htmlstruct' WHERE id=$id";
    mysqli_query($link,$sql);     //插入操作返回的是布尔值
    $result = mysqli_affected_rows($link);

    if($result){
        return 1;
    }else{
        return 0;
    }
}

function readResume($id){
    $link = initDBconnect();

    $sql = "SELECT name,htmlstruct FROM resume where id=$id";
    $result = mysqli_query($link,$sql);
    
    $row = mysqli_fetch_assoc($result);
    
    return $row;
}